<template>
    <h3>导航列表</h3>
    <!-- 表格 -->
    <div class="category-table">
        <el-table :data="tableData" stripe style="width: 100%">
            <el-table-column prop="index" label="序号" width="90" />
            <el-table-column prop="text" label="导航名称" width="180" />
            <el-table-column label="操作">
                <template #default="scope">
                    <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>


<script setup lang="ts">
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus';
import type { Action } from 'element-plus'
import api from '../../api/http'
const input = ref('')
const imageUrl = ref('')
const pic = ref('')
let imgArr = ref<any>([])

// 表格数据
const tableData = ref<any>([])
const handleDelete = (index: number, row: any) => {
    // console.log(index, row)
    let { id, banner, text } = row;
    ElMessageBox.confirm(
        `是否删除这个“${text}”轮播图?`,
        'Confirm',
        {
            distinguishCancelAndClose: true,
            confirmButtonText: '确定',
            cancelButtonText: '取消',
        }
    )
        .then(async () => {
            // console.log("点击确定")
            await api.nav_remove({ id })
                .then(
                    content => {
                        let { code, msg } = content;
                        if (code != 200) {
                            ElMessage.error(msg)
                        }
                    }
                )
            tableData.value.splice(index, 1);
        })
        .catch((action: Action) => {
            // console.log("点击取消")
        })

}
// 定义每一项数据的属性类型
type Item = {
    id: string
    text: string
    banner: string
    index?: string | number
}
const getNavListFn = async () => {
    await api.nav_list()
        .then(
            content => {
                // console.log(content)
                let { code, result } = content;
                if (code == 200) {
                    let arr = result.map((item: Item, index: number) => {
                        item.index = index + 1;
                        return item;
                    })
                    tableData.value = arr;
                }
            }
        )
}
getNavListFn();


</script>

<style lang="scss">
h3 {
    margin-bottom: 15px;
}

.category-table {
    margin-top: 15px;
}

.category-form {
    .opt {
        margin-top: 15px;
    }

}

.avatar-uploader-icon,
.el-upload-dragger,
.avatar-uploader,
.avatar {
    width: 178px;
    height: 178px;
    display: block;
    padding: 0 !important;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
    padding: 0 !important;
    line-height: 178px;
}
</style>